<template>
  <div id="DemoPage">
    <h1>RY-EDIT-TABLE</h1>
    <ry-edit-table
      ref="ryEditTable"
      :listData="listData"
      :listConfig="listConfig"
      :rowButtons="rowButtons"
      :operationsConfig="{ width: 180 }"
      :action="'action'"
      :cellStyle="{ color: 'orange' }"
      :cellClassName="'custom-cell-class'"
      trigger="onChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55px" fixed="left" />
      <el-table-column type="index" label="序号" width="100px" fixed="left" />
    </ry-edit-table>
  </div>
</template>

<script>
export default {
  name: "demoPage",

  data() {
    return {
      // 下拉资源集合
      dropDownOptions: {
        job: [],
      },
      // 模拟数据
      list: [
        {
          name: "Time1",
          age: "26",
          pkid: 1,

          date: "1998-01-30",
          job: "0",
          job$view: "运动员",
          items$view: "羽毛球，游泳",
          items: ["0", "3"],
        },
        {
          name: "cherry",
          age: "13",
          pkid: 2,

          date: "1996-01-30",
          job: "2",
          job$view: "学生",
          items$view: "羽毛球，游泳",
          items: ["0", "3"],
        },
        {
          name: "alex",
          age: "28",
          pkid: 3,

          date: "1992-01-30",
          job: "0",
          job$view: "运动员",
          items$view: "羽毛球，游泳",
          items: ["0", "3"],
        },
      ],
      // 表格数据
      listData: [],
      // 表头配置
      listConfig: [
        {
          mode: "text",
          label: "姓名",
          prop: "name",
          minWidth: "100px",
          disabled(row) {
            return row.name === "cherry" ? true : false;
          },
          change: (v, row, index) => {
            console.log("v, row, index: ", v, row, index);
          },
          blur: (v) => {
            console.log("name blur");
          },
          rules: [
            {
              type: "string",
              required: true,
              message: "姓名不能为空",
            },
          ],
        },
        {
          mode: "text",
          label: "年龄",
          prop: "age",
          minWidth: "100px",
          sortable: true,
          change: (v, row) => (row.job = v <= 22 ? "2" : ""),
          rules: [
            {
              type: "number",
              asyncValidator: (rule, value) => {
                return new Promise((resolve, reject) => {
                  if (value < 1) {
                    reject("年龄需要大于1岁");
                  } else {
                    resolve();
                  }
                });
              },
            },
          ],
        },
        {
          mode: "date",
          label: "出生日期",
          prop: "date",
          minWidth: "150px",
          inputConfig: {
            "value-format": "YYYY-MM-DD",
          },
          rules: [
            {
              type: "date",
              required: true,
              message: "出生日期不能为空",
            },
          ],
        },
        {
          mode: "select",
          label: "职业/身份",
          prop: "job",
          renderProp: "job$view",
          minWidth: "100px",
          placeholder: "请选择",
          inputConfig: {
            clearable: true,
          },
          options: (row) => {
            return row.age > 22
              ? this.dropDownOptions.job.filter((item) => item.value !== "2")
              : this.dropDownOptions.job.filter((item) => item.value === "2");
          },

          rules: [
            {
              required: true,
              message: "职业/身份不能为空",
            },
          ],
        },

        {
          mode: "select",
          label: "参与比赛项目",
          minWidth: "300px",
          prop: "items",
          renderProp: "items$view",
          placeholder: "多项选择",
          // spliter:' - ',
          inputConfig: {
            clearable: true,
            multiple: true,
          },
          options: [
            { value: "0", label: "羽毛球" },
            { value: "1", label: "篮球" },
            { value: "2", label: "乒乓球" },
            { value: "3", label: "游泳" },
          ],
          rules: [
            {
              required: true,
              message: "比赛项目不能为空",
            },
          ],
        },
      ],

      // 行按钮配置
      rowButtons: [
        {
          name: "编辑",
          type: "primary",
          vIf: (row) => !row.isEdit,
          click: (ref) => {
            ref.edit();
          },
        },
        {
          name: "保存",
          type: "success",
          vIf: (row) => row.isEdit,
          click: (ref, row) => {
            console.log("保存", row);
            ref.validate((valid, fields) => {
              if (valid) {
                console.log("校验通过");
                ref.cancel();
              } else {
                console.log("校验不通过", fields);
              }
            });
          },
        },
        {
          name: "取消",
          type: "danger",
          vIf: (row) => row.isEdit,
          click: (ref) => {
            ref.cancel();
          },
        },

        {
          name: "删除",
          type: "danger",
          vIf: (row) => !row.isEdit,
          disabled(row, index) {
            return row.name === "ryan";
          },
          click: (ref, row, index) => {
            ref.delete();
          },
        },
      ],
    };
  },
  created() {
    this.getDropDownOptions();
    this.getList();
  },
  methods: {
    getList() {
      setTimeout(() => {
        this.listData = [...this.list];
      }, 1000);
    },
    async getDropDownOptions() {
      this.dropDownOptions.job = await Promise.resolve([
        { label: "运动员", value: "0" },
        { label: "工程师", value: "1" },
        { label: "学生", value: "2" },
      ]);
    },
    // 插入新增行
    insert() {
      this.$refs.ryEditTable.insert({
        name: "",
        age: "",
        job: "",
        date: "",
        items: [],
      });
    },
  },
};
</script>

<style scoped></style>
